//
//  BYMineOrderView.m
//  OCDemo
//
//  Created by 朱慕之 on 2022/1/4.
//

#import "BYMineOrderView.h"
#import "BYMineOrderItem.h"
#import "BYOrderItemModel.h"


#define ORDERTITLE_ARRAY_NORMAL  @[@"全部订单",@"待付款",@"待发货",@"待收货"]

#define ORDERIMAGE_ARRAY_NORMAL    @[@"mine_allOrder",@"mine_waitPay",@"mine_waitSend",@"mine_waitReceiv"]

#define ORDERTITLE_ARRAY    @[@"全部订单",@"待付款",@"待发货",@"待收货", @"增加商品", @"删除商品", @"更新商品", @"待发货"]
#define ORDERIMAGE_ARRAY    @[@"mine_allOrder",@"mine_waitPay",@"mine_waitSend",@"mine_waitReceiv",@"mine_add",@"mine_dele",@"mine_update",@"mine_post"]

@interface BYMineOrderView ()<UICollectionViewDataSource, UICollectionViewDelegate>

/**
 * 背景
 */
@property (nonatomic , strong) UIView *backGround;

/**
 * 我的订单
 */
@property (nonatomic , strong) UILabel *myOrders;

/**
 * 全部订单
 */
@property (nonatomic , strong) UILabel *allOrders;

/**
 * 箭头
 */
@property (nonatomic , strong) UIImageView *arrow;

/**
 * 线
 */
@property (nonatomic , strong) UIView *line;

/**
 * 订单入口collectionView
 */
@property (nonatomic , strong) UICollectionView *collectionView;

/**
 * 订单入口数据源
 */
@property (nonatomic , strong) NSMutableArray *listArray;


@end


@implementation BYMineOrderView


static NSString *kMineOrderItemID = @"kMineOrderItemID";

#pragma mark - Event
- (void)tap:(UITapGestureRecognizer *)gesture {
    if (self.allOrderBlock) {
        self.allOrderBlock(@"全部订单");
    }
}

#pragma mark - InitData

- (void)handleOrderData {
    [self.listArray removeAllObjects];
    NSMutableArray *titleArray = [NSMutableArray array];
    NSMutableArray *imageArray = [NSMutableArray array];
    if ([AppConfig isAdministrator]) {
        titleArray = [NSMutableArray arrayWithArray:ORDERTITLE_ARRAY_NORMAL];
        imageArray = [NSMutableArray arrayWithArray:ORDERIMAGE_ARRAY_NORMAL];
    }else {
        titleArray = [NSMutableArray arrayWithArray:ORDERTITLE_ARRAY];
        imageArray = [NSMutableArray arrayWithArray:ORDERIMAGE_ARRAY];
    }

    for (NSInteger i = 0; i < titleArray.count; i++) {
        if (i == 0) {
            BYOrderItemModel *itemModel = [[BYOrderItemModel alloc] init];
            itemModel.orderIcon = @"mine_allOrder";
            itemModel.orderTitle = @"全部订单";
            [self.listArray addObject:itemModel];
        } else {
            BYOrderItemModel *itemModel = [[BYOrderItemModel alloc] init];
            itemModel.orderTitle =  titleArray[i];
            itemModel.orderIcon = imageArray[i];
            [self.listArray addObject:itemModel];
        }
    }   
}

#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = BACKGROUND_VIEW_COLOR;
        [self handleOrderData];
        [self addSubview:self.backGround];
        [self.backGround addSubview:self.arrow];
        [self.backGround addSubview:self.myOrders];
        [self.backGround addSubview:self.allOrders];
        [self.backGround addSubview:self.line];
        [self.backGround addSubview:self.collectionView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.backGround mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(kAutoWithSize(10));
        make.right.equalTo(self).offset(-kAutoWithSize(10));
        make.top.bottom.equalTo(self);
    }];
    
    [self.arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backGround).offset(kAutoWithSize(10));
        make.right.equalTo(self.backGround).offset(-kAutoWithSize(5));
        make.height.width.equalTo(@(kAutoWithSize(20)));
    }];
    
    [self.myOrders mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.arrow);
        make.left.equalTo(self.backGround).offset(kAutoWithSize(10));
    }];
    
    [self.allOrders mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.myOrders);
        make.right.equalTo(self.arrow.mas_left);
    }];
    
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myOrders.mas_bottom).offset(kAutoWithSize(10));
        make.left.right.equalTo(self.backGround);
        make.height.equalTo(@(1));
    }];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line.mas_bottom);
        make.left.right.equalTo(self.backGround);
        make.bottom.equalTo(self.backGround.mas_bottom).offset(-kAutoWithSize(10));
    }];
    
    [super layoutSubviews];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((kScreenWidth-kAutoWithSize(20))/4, kAutoWithSize(80));
}

#pragma mark - UICollectionViewDelegaqte & UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BYMineOrderItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMineOrderItemID forIndexPath:indexPath];
    BYOrderItemModel *itemModel = self.listArray[indexPath.row];
    cell.model = itemModel;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BYOrderItemModel *itemModel = self.listArray[indexPath.row];
    if (self.orderItemBlock) {
        self.orderItemBlock(itemModel.orderTitle);
    }
}


#pragma mark - get
- (NSMutableArray *)listArray {
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (UILabel *)myOrders {
    if (!_myOrders) {
        _myOrders = [[UILabel alloc] init];
        _myOrders.textColor = UIColorFromRGB(0x333333);
        _myOrders.font = MZBoldFont(kFont(14));
        _myOrders.text = @"我的订单";
    }
    return _myOrders;
}

- (UILabel *)allOrders {
    if (!_allOrders) {
        _allOrders = [[UILabel alloc] init];
        _allOrders.textAlignment = NSTextAlignmentRight;
        _allOrders.textColor = UIColorFromRGB(0x999999);
        _allOrders.font = MZFont(kFont(12));
        _allOrders.text = @"全部订单";
        _allOrders.userInteractionEnabled = YES;
        [_allOrders addTapAction:self selector:@selector(tap:)];
    }
    return _allOrders;
}

- (UIImageView *)arrow {
    if (!_arrow) {
        _arrow = [[UIImageView alloc] init];
        _arrow.image = [UIImage imageNamed:@"mine_cellArrow"];
        _arrow.contentMode = UIViewContentModeCenter;
        _arrow.userInteractionEnabled = YES;
        [_arrow addTapAction:self selector:@selector(tap:)];
    }
    return _arrow;
}

- (UIView *)backGround {
    if (!_backGround) {
        _backGround = [[UIView alloc]init];
        _backGround.backgroundColor = [UIColor whiteColor];
        [_backGround setViewCornerRadius:6.0];
    }
    return _backGround;
}

- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xF5F5F5);
    }
    return _line;
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        // 指定宽高
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.scrollEnabled = YES;
        _collectionView.backgroundColor = [UIColor whiteColor];
        [_collectionView registerClass:[BYMineOrderItem class] forCellWithReuseIdentifier:kMineOrderItemID];
    }
    return _collectionView;
}


@end
